<?php
namespace easemob;

class Message extends IM
{
    
    protected $target_type=['users','chatgroups','chatrooms'];
    
    
    protected  function send($data){
        $url=$this->geturl('messages');
        $ret=$this->sentPost($url, $data);
        return isset($ret['data'])?$ret['data']:false;
        
    }
    
    function sendTxt($content,$target,$from='',$type=0,$ext=[]){
        if(is_string($target)){
            $target=[$target];
        }
        $data=[        
            "target_type" => $this->target_type[$type], 
            "target" => $target,  
            "msg" => [
                "type" => "txt",
                "msg" => $content
            ]            
        ];
        
        if(!empty($from)){
            $data['from']=$from;
        }
        if(!empty($ext)){
            $data['ext']=$ext;
        }
        
        
        return $this->send($data);        
        
    }
    
    /**
     *  发送透传信息
     * @param string $content
     * @param array|string $target
     * @param string $from
     * @param number $type
     * @param array $ext
     * @return boolean
     */
    function sendCmd($content,$target,$from='',$type=0,$ext=[]){
        if(is_string($target)){
            $target=[$target];
        }
        $data=[
            "target_type" => $this->target_type[$type],
            "target" => $target,
            "msg" => [
                "type" => "cmd",
                "msg" => $content
            ]
        ];
        
        if(!empty($from)){
            $data['from']=$from;
        }
        if(!empty($ext)){
            $data['ext']=$ext;
        }
        
        
        return $this->send($data);
        
    }
    
    
    function sendImg($file,$target,$from='',$type=0,$ext=''){
        
        $ret=$this->uploadFile($file);
        $uri=$ret['uri'];
        $uuid=$ret['entities'][0]['uuid'];
        $shareSecret=$ret['entities'][0]['share-secret'];
        $data=[
            "target_type" => $this->target_type[$type],
            "target" => $target,
            "msg" => [
                "type" => "img",   // 消息类型
                "url"=> $uri.'/'.$uuid,
                "filename"=> $file, // 指定一个文件名
                "secret"=> $shareSecret, // 成功上传文件后返回的secret
                "size" => [
                    "width" => 480,
                    "height" => 720
                ]
            ]
        ];
        
        if(!empty($from)){
            $data['from']=$from;
        }
        
        return $this->send($data); 
    }
    
    /**
     * 上传文件
     * @param unknown $filePath
     * @return unknown
     */
    function uploadFile($filePath){
        $url=$this->geturl('chatfiles');
        $file=file_get_contents($filePath);
        $body['file']=$file;      
        $result=$this->sentUpload($url, $body);
        return $result;
        
    }
  
    function downloadFile($uuid,$shareSecret,$ext)
    {
        /* $url = $this->url . 'chatfiles/' . $uuid;
        $header = array("share-secret:" . $shareSecret, "Accept:application/octet-stream", $this->getToken(),);
        if ($ext=="png") {
            $result=$this->postCurl($url,'',$header,'GET');
        }else {
            $result = $this->getFile($url);
        }
        $filename = md5(time().mt_rand(10, 99)).".".$ext; //新图片名称
        if(!file_exists("resource/down")){
            mkdir("resource/down/");
        }
        $file = @fopen("resource/down/".$filename,"w+");//打开文件准备写入
        @fwrite($file,$result);//写入
        fclose($file);//关闭
        return $filename; */
        
    }
    
    /**
     * 获取下载聊天记录的地址
     * @param number $time  时间戳
     * @return boolean
     */
    function getMsgUrl($time=0){
        //GET /{org_name}/{app_name}/chatmessages/{time}
        if(empty($time)) $time=time()-3600;        
        $tm=date('YmdH',$time);        
        $url=$this->geturl('chatmessages/'.$tm);
        $ret=$this->sentGet($url);
        if(isset($ret['data'][0]['url'])){            
            return $ret['data'][0]['url'];
            
        }else{
            return false;
        }
        
    }
    
}